package org.insia.smartposse.struts.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.ICompanyDAO;
import org.insia.smartposse.metier.Company;
import org.insia.smartposse.utils.ActionUtils;
/**
 * Action for creating and modifying a company.
 * @author sok hout
 *
 */
public class CreateCompanyAction extends DispatchAction {
	
	protected final Logger log = Logger.getLogger(getClass());
	
    @Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
		Company company = new Company(-1L,"","","","",0,"","","","");
		request.setAttribute("company", company);
		request.setAttribute("messages", new String[]{});
		return mapping.findForward("show");
	}

	public ActionForward createCompany(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
    	// initialisation de la liste des mesages d'erreurs
    	List<String> messages = new ArrayList<String>();
    	request.setAttribute("messages", messages);
    	
    	// recupération des données
    	Long id = ActionUtils.getLong(request, "id", -1L, messages, "company.error.id");
    	Integer gender = ActionUtils.getInt(request, "gender", 0, messages, "company.error.gender");
    	String firstName = ActionUtils.getTrimedString(request, "firstName","", messages, "company.error.firstName");
    	String lastName = ActionUtils.getTrimedString(request, "lastName","", messages, "company.error.lastName");
    	String login = ActionUtils.getTrimedString(request, "login","", messages, "company.error.login");
    	String 	password = ActionUtils.getTrimedString(request, "password","", messages, "company.error.password");
    	String email = ActionUtils.getEmail(request, "email","", messages, "company.error.email");
    	
    	String url = ActionUtils.getTrimedString(request, "url","", null, null);
    	String name = ActionUtils.getTrimedString(request, "name","", messages, "company.error.name");
    	String address = ActionUtils.getTrimedString(request, "address","", null, null);

    	Company company;
    	
    	// S'il y a eu des erreurs
    	if (messages.size() > 0) {
    		company = new Company (id, login, password, firstName, lastName, gender, email, name, address, url);
    		request.setAttribute("company", company);
    		return mapping.findForward("failure");
    	}
    	
		ICompanyDAO dao = DAOFactory.getCompanyDAO();
    	// Si creation de nouveau company.
    	if (id == -1L) {
    		log.info("dans la partie insert");
    		company = new Company (id, login, password, firstName, lastName, gender, email, name, address, url);
    		dao.create(company);
    		request.setAttribute("msgTitleKey", "company.createSuccessTitle");
    		request.setAttribute("msgLabelKey", "company.createSuccessLabel");
    	} else {
    		log.info("dans la partie update");
    		//Sinon modification de company
    		company = dao.get(id);
    		company = new Company (id, login, password, firstName, lastName, gender, email, name, address, url);
    		dao.update(company);
    		request.setAttribute("msgTitleKey", "company.updateSuccessTitle");
    		request.setAttribute("msgLabelKey", "company.updateSuccessLabel");
    	}
    	return mapping.findForward("success");
    }
}